import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import Layout from '../layout/index.vue'

export const routes = [
  {
    path: '/login',
    hidden: true,
    component: () => import('../views/login/index.vue')
  },
  {
    path: '',
    component: Layout,
    meta: { title: '首页', icon: 'House' },
    children: [
      {
        path: '',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '首页' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: 'user',
        component: () => import('../views/system/user/index.vue'),
        meta: { title: '用户管理' }
      }
    ]
  },
  {
    path: '/medical',
    component: Layout,
    meta: { title: '医疗管理', icon: 'FirstAidKit' },
    children: [
      {
        path: 'doctor',
        name: 'DoctorList',
        component: () => import('../views/doctor/index.vue'),
        meta: { title: '医生列表' }
      },
      // {
      //   path: 'appointment',
      //   name: 'Appointment',
      //   component: () => import('../views/appointment/index.vue'),
      //   meta: { title: '预约管理' }
      // }
    ]
  },
  {
    path: '/article',
    component: Layout,
    meta: { title: '文章管理', icon: 'Document' },
    children: [
      {
        path: '',
        name: 'Article',
        component: () => import('../views/article/index.vue'),
        meta: { title: '文章列表' }
      },
      {
        path: 'edit/:id?',
        name: 'ArticleEdit',
        component: () => import('../views/article/edit.vue'),
        // meta: { title: '新增文章', activeMenu: '/article' },
        hidden: true
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = userStore.token

  if (token) {
    to.path === '/login' ? next('/') : next()
  } else {
    to.path === '/login' ? next() : next('/login')
  }
})

export default router
